package lcr;

public class Test0027 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);

        Solution0027_1 solution = new Solution0027_1();
        System.out.println(solution.isPalindrome(head));
    }
}
class Solution0027_1 {
    public boolean isPalindrome(ListNode head) {
        ListNode fast = head, slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }

        slow = reverseList(slow);

        while (slow != null){
            if (head.val != slow.val) return false;
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public ListNode reverseList(ListNode head) {
        ListNode ans = new ListNode();
        while (head!=null){
            ListNode temp = head.next;
            head.next=ans.next;
            ans.next=head;
            head=temp;
        }
        return ans.next;
    }
}
